package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommendlQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JasonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/travelCommend")
public class TravelCommendController {
    //注入service对象
    @Autowired
    private ITravelCommendService travelCommendService ;

    /**
     * 新增推荐游记或修改
     //新增完之后就跳到主页面
     */
    @RequestMapping("/saveOrUpdate.do")
    public String saveOrUpdate(TravelCommend travelCommend, MultipartFile img){
        //判断一下文件有没有上传过来
        if (img!=null&&img.getContentType().startsWith("image")&&img.getSize()>0) {
            //如果有上传图片的文件过来的话就将原来的封面替换掉
            String url = UploadUtil.upload(img);
            travelCommend.setCoverUrl(url);
        }
        //执行新增或者修改的语句
        travelCommendService.saveOrUpdate(travelCommend);
        //重新跳到主页面
        return "redirect:/travelCommend/list.do" ;
    }

    /**
     * 查询所有的游记
     */
    @RequestMapping("/list.do")
    public String list(Model model, @ModelAttribute("qo") TravelCommendlQueryObject qo){

        //在这边设置高级查询的条件
        qo.setOrderBy("schedule desc");//按推荐时间的倒序去排序查找
        model.addAttribute("pageInfo",travelCommendService.query(qo));
        return "travelCommend/list" ;
    }
}
